<?php

namespace app\api\controller;

use app\common\controller\Ucenter;

/**
 * 个人中心接口
 */
class User extends Ucenter
{
    protected $noNeedLogin = ['*'];
    protected $noNeedRight = ['*'];

    /**
     * 我的资料
     *
     */
    public function my_user_info()
    {
        if (\request()->isGet())
        {
            $my_enterprise_info = Model('enterprise')->field('id,name,enterprise_type')->find(['user_id' => $this->user_info['id']]);
            $enterprise_type = Model('enterprise')->find(['id' => $my_enterprise_info['enterprise_type']]);
            $my_enterprise_info['enterprise_type'] = $enterprise_type['name'];
            $this->success("ok", $my_enterprise_info);
        }
    }

    /**
     * 修改我的资料
     *
     */
    public function edit_my_user_info()
    {
        if (\request()->isPost())
        {
            $params = $this->request->post();
            $data = [
                'name' => empty($params['name']) ? '' : trim($params['name']),
                'native_place' => empty($params['native_place']) ? '' : trim($params['native_place']),
                'educational_background' => empty($params['educational_background']) ? '' : trim($params['educational_background']),
                'school' => empty($params['school']) ? '' : trim($params['school']),
                'mobile' => empty($params['mobile']) ? '' : trim($params['mobile']),
                'is_public_mobile' => in_array($params['is_public_mobile'],[0,1])? 0 : $params['is_public_mobile'],
            ];

            Model('user')->where('id',$this->user_info['id'])->update($data);
            $this->success("ok");
        }
    }

    /**
     * 我的企业
     *
     */
    public function my_enterprise_detail()
    {
        if (\request()->isGet())
        {
            $data = Model('enterprise')->find(['user_id' => $this->user_info['id']]);
            $this->success("ok", $data);
        }
    }

    /**
     * 我的需求
     *
     */
    public function my_requirement_list()
    {
        if (\request()->isGet())
        {
            $requirement_model = Model('requirement')->where('user_id',$this->user_info['id']);

            if($status = $this->request->get('status',0))
            {
                if(in_array($status,[0,1]))
                {
                    $requirement_model->where('status',$status);
                }
            }

            $page = $this->request->get('status',1);
            $page_limit = $this->request->get('page_limit',10);

            $list = $requirement_model->paginate(['page' => $page, 'list_rows' => $page_limit]);
            if(!empty($list))
            {
                foreach($list as $key => $value)
                {
                    $list[$key]['created_time'] = date('Y-m-d H:i:s',$value['created_time']);
                    $list[$key]['updated_time'] = date('Y-m-d H:i:s',$value['updated_time']);
                }
            }

            $this->success("ok", $list);
        }
    }

    /**
     * 发布需求
     *
     */
    public function add_requirement()
    {
        if (\request()->isPost())
        {
            $params = $this->request->post();
            $data = [
                'user_id' => $this->user_info['id'],
                'name' => empty($params['name']) ? '' : trim($params['name']),
                'mobile' => empty($params['mobile']) ? '' : trim($params['mobile']),
                'text' => empty($params['text']) ? '' : trim($params['text']),
                'status' => 0,
            ];

            Model('requirement')->insert($data);
            $this->success("ok");
        }
    }

    /**
     * 我的活动
     *
     */
    public function my_activity()
    {
        if (\request()->isGet())
        {

            $page = $this->request->get('status',1);
            $page_limit = $this->request->get('page_limit',10);

            $start = ($page-1) * $page_limit;
            $sql = "select * from activity where find_in_set('{$this->user_info['id']}', member) limit {$start},{$page_limit}";
            $list = Model('activity')->query($sql);

            if(!empty($list))
            {
                foreach($list as $key => $value)
                {
                    $list[$key]['created_time'] = date('Y-m-d H:i:s',$value['created_time']);
                    $list[$key]['updated_time'] = date('Y-m-d H:i:s',$value['updated_time']);
                }
            }

            $this->success("ok", $list);
        }
    }

    /**
     * 我的项目
     *
     */
    public function my_project()
    {
        if (\request()->isGet())
        {

            $page = $this->request->get('status',1);
            $page_limit = $this->request->get('page_limit',10);

            $start = ($page-1) * $page_limit;
            $sql = "select * from project where find_in_set('{$this->user_info['id']}', cooperate_member) limit {$start},{$page_limit}";
            $list = Model('project')->query($sql);

            if(!empty($list))
            {
                foreach($list as $key => $value)
                {
                    $list[$key]['created_time'] = date('Y-m-d H:i:s',$value['created_time']);
                    $list[$key]['updated_time'] = date('Y-m-d H:i:s',$value['updated_time']);
                }
            }

            $this->success("ok", $list);
        }
    }
}